﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_BaiThi_DanhSachBaiThi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrv();
          //  LoadDDL();
        }
    }

    private void LoadGrv()
    {
        List<LopINFO> dslophoc = LopDAL.listAll();
        ddllophoc.DataSource = dslophoc;
        ddllophoc.DataTextField = "TenLop";
        ddllophoc.DataValueField = "MaLop";
        ddllophoc.DataBind();
        ddllophoc.Items.Insert(0, new ListItem("Chọn lớp học", "0"));

        ddllophoc1.DataSource = dslophoc;
        ddllophoc1.DataTextField = "TenLop";
        ddllophoc1.DataValueField = "MaLop";
        ddllophoc1.DataBind();
        ddllophoc1.Items.Insert(0, new ListItem("Chọn lớp học", "0"));

        List<DeThiINFO> dsDethi = DeThiDAL.listAll();
        ddldethi.DataSource = dsDethi;
        ddldethi.DataTextField = "TenDe";
        ddldethi.DataValueField = "MaDe";
        ddldethi.DataBind();
        ddldethi.Items.Insert(0, new ListItem("Chọn đề thi", "0"));

        ddlDethiS.Visible = false;
        lblDeThi.Visible = false;
    }
    private void LoadDDL()
    {
        int ma = int.Parse(ddllophoc.SelectedValue.ToString());
        int MaDe = int.Parse(ddlDethiS.SelectedValue.ToString());
        DataTable ds = BaiThiDAL.timkiem(ma, MaDe);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void ddllophoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ma = int.Parse(ddllophoc.SelectedValue.ToString());
        List<DeThiINFO> dsDethi = BaiThiDAL.ListDeThiLop(ma);
        if (ma > 0 && dsDethi.Count>0)
        {
            ltrbtnKhoaBaiThi.Text = "<button type='button' class='btn btn-success btn-xs' onclick='return khoabaithi();'>Khóa bài thi</button>";
            ltrbtnKhoaBaiThi.Text += " <button type='button' class='btn btn-warning btn-xs' onclick='return showAlertDeleteAll();'>Xóa tất cả bài thi</button>";
            ltrbtnKhoaBaiThi.Text += " <button type='button' class='btn btn-info btn-xs' onclick='return taoMatkhau();'> Gửi mật khẩu</button>";
            ddlDethiS.DataSource = dsDethi;
            ddlDethiS.DataTextField = "TenDe";
            ddlDethiS.DataValueField = "MaDe";
            ddlDethiS.DataBind();
            ddlDethiS.SelectedIndex = 0;
            LoadDDL();
            ddlDethiS.Visible = true;
            lblDeThi.Visible = true;
        }else {
            ddlDethiS.Visible = false;
            ltrbtnKhoaBaiThi.Text = "";
            lblDeThi.Visible = false;
            GridView1.DataSource = null;
            DataBind();
        }
    }
    protected void refresh_Click(object sender, EventArgs e)
    {
        LoadDDL();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView obj = (DataRowView)e.Row.DataItem;
            string trangthai = "<span class='label label-success'>Cho thi</span>";
            if (obj["TinhTrang"].ToString().Equals("2"))
            {
                trangthai = "<span class='label label-warning'>Đang thi</span>";
            }
            else if (obj["TinhTrang"].ToString().Equals("3"))
            {
                trangthai = "<span class='label label-info'>Thi xong</span>";
            }
            else if (obj["TinhTrang"].ToString().Equals("4"))
            {
                trangthai = "<span class='label label-danger'>Bỏ thi</span>";
            }
            Literal lbltrangthai = (Literal)e.Row.FindControl("ltrTrangThai");
            lbltrangthai.Text = trangthai;
        }
    }
    protected void ddlDethiS_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDDL();
    }
}